﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helper.Business.Implementation;
using Helper.Business.Repository;
using Helper.Core.Entity;
using Helper.Reporting.Objects.User;
using System.IO;
using CrystalDecisions.Shared;
using Helper.Infrastructure.Reporting;
using CrystalDecisions.CrystalReports.Engine;

namespace Helper.Controllers
{
    public partial class UserController : BaseController
    {
        IUserRepository _UserRepository;

        public UserController()
        {
            LoginNeeded = true;
            if (_UserRepository == null)
                _UserRepository = new UserRepository();
        }

        //
        // GET: /User/

        public ActionResult Index()
        {
            IList<User> lstUser = _UserRepository.GetAll();
            return View(lstUser);
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {            
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(User collection)
        {
            try
            {
                _UserRepository.Add(collection);
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
                return View();
            }
        }

        //GET: /User/Create

        public ActionResult Create()
        {
            ViewBag.BusinessList = new BusinessRepository().GetAll();
            ViewBag.SectorList = new SectorRepository().GetAll();
            return View();
        }

        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(int id)
        {
            Metadata.UserMetadata _userMeta = new Metadata.UserMetadata();
            
            User usu = _UserRepository.FindById(id);
            _userMeta.Id = id;
            _userMeta.Name = usu.Name;
            _userMeta.ParentBusiness = usu.ParentBusiness;
            _userMeta.Sector = usu.Sector;
            _userMeta.Role = usu.Role;

            ViewBag.BusinessList = new BusinessRepository().GetAll();
            ViewBag.SectorList = new SectorRepository().GetAll();

            return View(_userMeta);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int Id, User collection)
        {
            try
            {
                User newUser = _UserRepository.FindById(Id);
                newUser.Name = collection.Name;
                newUser.Password = collection.Password;
                newUser.ParentBusiness = new BusinessRepository().FindById(collection.ParentBusiness.Id);
                newUser.Sector = new SectorRepository().FindById(collection.Sector.Id);
                newUser.Role = collection.Role;
                _UserRepository.Update(newUser);
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                return View();
            }
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(int id)
        {
            User delUser = _UserRepository.FindById(id);
            return View(delUser);
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, User collection)
        {
            try
            {
                _UserRepository.Delete(_UserRepository.FindById(id)); 
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //GET /User/Report/
        public ActionResult Report(UserReport Report, ReportType ReturnType)
        {
            switch (Report)
            {
                case UserReport.ShowAll:
                    rptShowAllUsers rptSAU = new rptShowAllUsers();
                    rptSAU.SetDataSource(_UserRepository.GetAll());
                    Streaming str = Streaming.GetFileFromReport((ReportClass)rptSAU, ReturnType);
                    return File(str.StreamReport, str.MimeType);
                case UserReport.ShowAllByBusiness:
                    rptShowAllUsersByBusiness rptSAUBB = new rptShowAllUsersByBusiness();
                    rptSAUBB.SetDataSource(_UserRepository.GetAll());
                    Streaming strSAUBB = Streaming.GetFileFromReport((ReportClass)rptSAUBB, ReturnType);
                    return File(strSAUBB.StreamReport, strSAUBB.MimeType);
                default:
                    return File(Stream.Null, "application/pdf");
            }
            
        }        
    }
}
